home *** CD-ROM | disk | FTP | other *** search
- package com.sun.java.swing.text;
-
- import com.sun.java.swing.plaf.TextUI;
- import java.awt.Graphics;
- import java.awt.Insets;
- import java.awt.Rectangle;
- import java.util.Vector;
-
- public class DefaultHighlighter implements Highlighter {
- private Vector highlights = new Vector();
- private JTextComponent component;
-
- public Object addHighlight(int p0, int p1, Highlighter.HighlightPainter p) throws BadLocationException {
- Document doc = this.component.getDocument();
- TextUI mapper = this.component.getUI();
- HighlightInfo i = new HighlightInfo(this);
- i.painter = p;
- i.p0 = doc.createPosition(p0);
- i.p1 = doc.createPosition(p1);
- this.highlights.addElement(i);
- mapper.damageRange(p0, p1);
- return i;
- }
-
- public void changeHighlight(Object tag, int p0, int p1) throws BadLocationException {
- TextUI mapper = this.component.getUI();
- Document doc = this.component.getDocument();
- HighlightInfo info = (HighlightInfo)tag;
- int oldP0 = info.p0.getOffset();
- int oldP1 = info.p1.getOffset();
- if (p0 == oldP0) {
- mapper.damageRange(Math.min(oldP1, p1), Math.max(oldP1, p1));
- } else if (p1 == oldP1) {
- mapper.damageRange(Math.min(p0, oldP0), Math.max(p0, oldP0));
- } else {
- mapper.damageRange(oldP0, oldP1);
- mapper.damageRange(p0, p1);
- }
-
- info.p0 = doc.createPosition(p0);
- info.p1 = doc.createPosition(p1);
- }
-
- public void deinstall(JTextComponent c) {
- this.component = null;
- }
-
- public Highlighter.Highlight[] getHighlights() {
- Highlighter.Highlight[] h = new Highlighter.Highlight[this.highlights.size()];
- this.highlights.copyInto(h);
- return h;
- }
-
- public void install(JTextComponent c) {
- this.component = c;
- this.removeAllHighlights();
- }
-
- public void paint(Graphics g) {
- Rectangle a = new Rectangle(this.component.getSize());
- Insets insets = this.component.getInsets();
- a.x += insets.left;
- a.y += insets.top;
- a.width -= insets.left + insets.right;
- a.height -= insets.top + insets.bottom;
- int len = this.highlights.size();
-
- for(int i = 0; i < len; ++i) {
- HighlightInfo info = (HighlightInfo)this.highlights.elementAt(i);
- Highlighter.HighlightPainter p = info.getPainter();
- p.paint(g, info.getStartOffset(), info.getEndOffset(), a, this.component);
- }
-
- }
-
- public void removeAllHighlights() {
- TextUI mapper = this.component.getUI();
- if (mapper != null) {
- int len = this.highlights.size();
- if (len != 0) {
- int p0 = Integer.MAX_VALUE;
- int p1 = 0;
-
- for(int i = 0; i < len; ++i) {
- HighlightInfo info = (HighlightInfo)this.highlights.elementAt(i);
- p0 = Math.min(p0, info.p0.getOffset());
- p1 = Math.max(p1, info.p1.getOffset());
- }
-
- mapper.damageRange(p0, p1);
- this.highlights.removeAllElements();
- }
- }
-
- }
-
- public void removeHighlight(Object tag) {
- TextUI mapper = this.component.getUI();
- HighlightInfo info = (HighlightInfo)tag;
- mapper.damageRange(info.p0.getOffset(), info.p1.getOffset());
- this.highlights.removeElement(tag);
- }
- }
-